package HandleDB;

import java.sql.SQLException;



import Communication.Client;
import Entity.Worker;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

public class WorkerQuery {

	private static Connection conn = (Connection) ConnectionManage
			.getInstance().getConnection();

	// connection to the DB

	public Worker getRow(Client client, String username,String Password)
			throws SQLException {

		String sql = "SELECT * FROM workers WHERE UserName=? AND Password=?";
		ResultSet rs = null;
		Worker tmp = new Worker();

		try (PreparedStatement stmt = (PreparedStatement) conn
				.prepareStatement(sql);) {
			stmt.setString(1 ,username);
			stmt.setString(2 ,Password);
			rs = (ResultSet) stmt.executeQuery(sql);

			if (rs.next()) {
				tmp.setWorkerID(rs.getString("WorkerID"));
				tmp.setUsername(rs.getString("UserName"));
				tmp.setFirstName(rs.getString("firstName"));
				tmp.setLastName(rs.getString("lastName"));
				tmp.setEmail(rs.getString("Email"));
				tmp.setPassword(rs.getString("Password"));
				tmp.setRole(rs.getInt("Role"));
				tmp.setStatus(rs.getInt("Status"));
			
				return tmp;
			} else {
				return null;
			}

		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}
	}
	
	public void setStatusInDB( Client client ,String workerid, int status)
			throws SQLException {

		String sql = "UPDATE workers SET Status=? WHERE WorkerID="+workerid+";";
		ResultSet rs = null;
		Worker tmp = new Worker();

		try (PreparedStatement stmt = (PreparedStatement) conn
				.prepareStatement(sql);) {
			stmt.setString(1, getstatus);
			stmt.setString(2 ,password);
			stmt.setInt(1, status);
			
			rs = (ResultSet) stmt.executeQuery();

			
			}
	}
	
}
